


package com.legstar.test.coxb.tcobwvb.bind;

import com.legstar.coxb.ICobolBinding;
import com.legstar.coxb.common.CComplexBinding;
import com.legstar.coxb.ICobolChoiceBinding;
import com.legstar.coxb.ICobolPackedDecimalBinding;
import com.legstar.coxb.CobolBindingFactory;
import com.legstar.coxb.ICobolBindingFactory;
import java.math.BigDecimal;
import com.legstar.coxb.ICobolStringBinding;
import com.legstar.coxb.ICobolComplexBinding;
import com.legstar.coxb.host.HostException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.legstar.test.coxb.tcobwvb.Transaction;
import com.legstar.test.coxb.tcobwvb.ObjectFactory;

/**
 * LegStar Binding for Complex element :
 *   Transaction.
 * 
 * This class was generated by LegStar Binding generator.
 */
public class TransactionBinding 
             extends CComplexBinding {

    /** Value object to which this cobol complex element is bound. */
    private Transaction mValueObject;
  
    /** Indicates that the associated Value object just came from the constructor
     * and doesn't need to be recreated. */
    private boolean mUnusedValueObject = false;
    
    /** Maximum host bytes size for this complex object. */
    private static final int BYTE_LENGTH = 25;
    
    /** Unbound child. */
    public ICobolChoiceBinding _transactionDateChoice;
    /** Child bound to value object property TransactionAmount(BigDecimal). */
    public ICobolPackedDecimalBinding _transactionAmount;
    /** Child bound to value object property TransactionComment(String). */
    public ICobolStringBinding _transactionComment;
            
    /** Logger. */
    private final Log _log = LogFactory.getLog(getClass());

    /** Binding factory. */
    private static final ICobolBindingFactory BF
        = CobolBindingFactory.getBindingFactory();
 
    /** Static reference to Value object factory to be used as default. */
    private static final ObjectFactory JF = new ObjectFactory();
    
    /** Current Value object factory (Defaults to the static one but can be
     *  changed). */
    private ObjectFactory mValueObjectFactory = JF;
    
    /**
     * Constructor for a root Complex element without a bound Value object.
     */
    public TransactionBinding() {
        this(null);
    }

    /**
     * Constructor for a root Complex element with a bound Value object.
     * 
     * @param valueObject the concrete Value object instance bound to this
     *        complex element
     */
    public TransactionBinding(
            final Transaction valueObject) {
        this("", "", null, valueObject);
    }

    /**
    * Constructor for a Complex element as a child of another element and
    * an associated Value object.
    * 
    * @param bindingName the identifier for this binding
    * @param fieldName field name in parent Value object
    * @param valueObject the concrete Value object instance bound to this
    *        complex element
    * @param parentBinding a reference to the parent binding
    */
    public TransactionBinding(
            final String bindingName,
            final String fieldName,
            final ICobolComplexBinding parentBinding,
            final Transaction valueObject) {
        
        super(bindingName, fieldName, Transaction.class, null, parentBinding);
        mValueObject = valueObject;
        if (mValueObject != null) {
            mUnusedValueObject = true;
        }
        initChildren();
        setByteLength(BYTE_LENGTH);
    }

    /** Creates a binding property for each child. */
    private void initChildren() {
        if (_log.isDebugEnabled()) {
            _log.debug("Initializing started");
        }
        /* Create binding children instances */

        _transactionDateChoice = new TransactionDateChoiceBinding("TransactionDateChoice", this);
        _transactionDateChoice.setCobolName("TRANSACTION-DATE");
        _transactionDateChoice.setByteLength(8);
        _transactionAmount = BF.createPackedDecimalBinding("TransactionAmount",
               "TransactionAmount", BigDecimal.class, this);
        _transactionAmount.setCobolName("TRANSACTION-AMOUNT");
        _transactionAmount.setByteLength(8);
        _transactionAmount.setTotalDigits(15);
        _transactionAmount.setFractionDigits(2);
        _transactionAmount.setIsSigned(true);
        _transactionComment = BF.createStringBinding("TransactionComment",
               "TransactionComment", String.class, this);
        _transactionComment.setCobolName("TRANSACTION-COMMENT");
        _transactionComment.setByteLength(9);

        /* Add children to children list */
        getChildrenList().add(_transactionDateChoice);
        getChildrenList().add(_transactionAmount);
        getChildrenList().add(_transactionComment);
 
        if (_log.isDebugEnabled()) {
            _log.debug("Initializing successful");
        }
    }
    
    /** {@inheritDoc} */
    public void createValueObject() throws HostException {
        /* Since this complex binding has a constructor that takes a
         * Value object, we might already have a Value object that
         * was not used yet. */
        if (mUnusedValueObject && mValueObject != null) {
            mUnusedValueObject = false;
            return;
        }
        mValueObject = mValueObjectFactory.createTransaction();
    }

    /** {@inheritDoc} */
    public void setChildrenValues() throws HostException {

         /* Make sure there is an associated Value object*/
        if (mValueObject == null) {
            createValueObject();
        }
        /* Get Value object property _transactionAmount */
        if (_log.isDebugEnabled()) {
            _log.debug("Getting value from Value object property "
                    + "_transactionAmount"
                    + " value=" + mValueObject.getTransactionAmount());
        }
        _transactionAmount.setObjectValue(mValueObject.getTransactionAmount());
        /* Get Value object property _transactionComment */
        if (_log.isDebugEnabled()) {
            _log.debug("Getting value from Value object property "
                    + "_transactionComment"
                    + " value=" + mValueObject.getTransactionComment());
        }
        _transactionComment.setObjectValue(mValueObject.getTransactionComment());
     }

    /** {@inheritDoc} */
    public void setPropertyValue(final int index) throws HostException {
 
        ICobolBinding child = getChildrenList().get(index);
        
       /* Children that are not bound to a value object are ignored.
        * This includes Choices and dynamically generated counters
        * for instance.  */
        if (!child.isBound()) {
            return;
        }
        
        /* Set the Value object property value from binding object */
        Object bindingValue = null;
        switch (index) {
        case 0:
            break;
        case 1:
            bindingValue = child.getObjectValue(BigDecimal.class);
            mValueObject.setTransactionAmount((BigDecimal) bindingValue);
            break;
        case 2:
            bindingValue = child.getObjectValue(String.class);
            mValueObject.setTransactionComment((String) bindingValue);
            break;
         default:
            break;
        }
        if (_log.isDebugEnabled()) {
            _log.debug("Setting value of Value object property "
                    + child.getJaxbName()
                    + " value=" + bindingValue);
        }
    }

    /** {@inheritDoc} */
    public Object getObjectValue(
            final Class < ? > type) throws HostException {
        if (type.equals(Transaction.class)) {
            return mValueObject;
        } else {
            throw new HostException("Attempt to get binding " + getBindingName()
                    + " as an incompatible type " + type);
        }
    }

    /** {@inheritDoc} */
    public void setObjectValue(
            final Object bindingValue) throws HostException {
        if (bindingValue == null) {
            mValueObject = null;
            return;
        }
        if (bindingValue.getClass().equals(Transaction.class)) {
            mValueObject = (Transaction) bindingValue;
        } else {
            throw new HostException("Attempt to set binding " + getBindingName()
                    + " from an incompatible value " + bindingValue);
        }
    }

    /**
     * @return the java object factory for objects creation
     */
    public ObjectFactory getObjectFactory() {
        return mValueObjectFactory;
    }

    /**
     * @param valueObjectFactory the java object factory for objects creation 
     */
    public void setObjectFactory(final Object valueObjectFactory) {
        mValueObjectFactory = (ObjectFactory) valueObjectFactory;
    }

    /** {@inheritDoc} */
    public boolean isSet() {
        return (mValueObject != null);
    }

    /**
     * @return the bound Value object
     */
    public Transaction getTransaction() {
        return mValueObject;
    }
    
}

